package com.bootdo.system.controller;

import java.util.List;
import java.util.Map;

import com.bootdo.system.domain.UserDO;
import com.bootdo.system.domain.UserInnerDO;
import com.bootdo.system.service.UserInnerService;
import com.bootdo.system.service.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.system.domain.ProjectDO;
import com.bootdo.system.service.ProjectService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

import javax.servlet.http.HttpServletRequest;

/**
 * 项目表
 * 
 * @author chglee
 * @email 1992lcg@163.com
 * @date 2020-06-24 19:11:08
 */
 
@Controller
@RequestMapping("/system/project")
public class ProjectController {
	@Autowired
	private ProjectService projectService;
	@Autowired
	private UserInnerService userInnerService;
	@Autowired
	private UserService userService;
	
	@GetMapping()
	@RequiresPermissions("system:project:project")
	String Project(){
	    return "system/project/project";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("system:project:project")
	public PageUtils list(@RequestParam Map<String, Object> params,HttpServletRequest request){
		System.out.println("项目参数="+params);

		String username = request.getSession().getAttribute("username").toString();
		UserDO userDo = userService.getUserByUsername(username);
		System.out.println("userdo="+userDo.getUserId());
		params.put("userId",userDo.getUserId());
		//查询列表数据
        Query query = new Query(params);
		List<ProjectDO> projectList = projectService.list(query);
		params.put("username",userDo.getUsername());

		params.put("limit",Integer.MAX_VALUE);
		params.put("offset",0);
		int total = projectService.list(params).size();
		System.out.println("项目数据库的总数="+total+" 计算的size是="+projectList.size());
		PageUtils pageUtils = new PageUtils(projectList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("system:project:add")
	String add(){
	    return "system/project/add";
	}

	@GetMapping("/edit/{projectId}")
	@RequiresPermissions("system:project:edit")
	String edit(@PathVariable("projectId") Integer projectId,Model model){
		ProjectDO project = projectService.get(projectId);
		model.addAttribute("project", project);
	    return "system/project/edit";
	}
	
	/**
	 * 保存
	 */
	@Transactional
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("system:project:add")
	public R save( ProjectDO project,HttpServletRequest request){

		String username = request.getSession().getAttribute("username").toString();
		project.setUserId(username);

		UserDO userDO = userService.getUserByUsername(username);

		UserInnerDO userInnerDO = new UserInnerDO();
		userInnerDO.setUserId(userDO.getUserId());


		if(projectService.save(project)>0){
			userInnerDO.setProjectId(project.getProjectId());
			System.out.println("新增的ProjectId="+userInnerDO.getProjectId());
			userInnerService.save(userInnerDO);

			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("system:project:edit")
	public R update( ProjectDO project){
		projectService.update(project);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("system:project:remove")
	public R remove( Integer projectId){
		if(projectService.remove(projectId)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("system:project:batchRemove")
	public R remove(@RequestParam("ids[]") Integer[] projectIds){
		projectService.batchRemove(projectIds);
		return R.ok();
	}
	
}
